home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / TMPNAM.C < prev    next >
C/C++ Source or Header  |  1992-03-02  |  3KB  |  91 lines

  1. /* This is file TMPNAM.C */
  2. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  3. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  4. ** Rochester NH, 03867-2954, USA.
  5. */
  6.  
  7. /*
  8.  * Copyright (c) 1988 Regents of the University of California.
  9.  * All rights reserved.
  10.  *
  11.  * Redistribution and use in source and binary forms are permitted provided
  12.  * that: (1) source distributions retain this entire copyright notice and
  13.  * comment, and (2) distributions including binaries display the following
  14.  * acknowledgement:  ``This product includes software developed by the
  15.  * University of California, Berkeley and its contributors'' in the
  16.  * documentation or other materials provided with the distribution and in
  17.  * all advertising materials mentioning features or use of this software.
  18.  * Neither the name of the University nor the names of its contributors may
  19.  * be used to endorse or promote products derived from this software without
  20.  * specific prior written permission.
  21.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  22.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  23.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  24.  */
  25.  
  26. #if defined(LIBC_SCCS) && !defined(lint)
  27. static char sccsid[] = "@(#)tmpnam.c    4.8 (Berkeley) 6/22/90";
  28. #endif /* LIBC_SCCS and not lint */
  29.  
  30. #include <sys/param.h>
  31. #include <sys/types.h>
  32. #include <stdio.h>
  33.  
  34. #ifndef P_tmpdir
  35. #define    P_tmpdir    "/usr/tmp"
  36. #endif
  37.  
  38. FILE *
  39. tmpfile()
  40. {
  41.     FILE *fp;
  42.     char *f, *tmpnam();
  43.  
  44.     if (!(f = tmpnam((char *)NULL)) || !(fp = fopen(f, "w+"))) {
  45.         fprintf(stderr, "tmpfile: cannot open %s.\n", f);
  46.         return(NULL);
  47.     }
  48.     (void)unlink(f);
  49.     return(fp);
  50. }
  51.  
  52. char *
  53. tmpnam(s)
  54.     char *s;
  55. {
  56.     char *malloc(), *mktemp();
  57.  
  58.     if (!s && !(s = malloc((u_int)MAXPATHLEN)))
  59.         return(NULL);
  60.     (void)sprintf(s, "%s/XXXXXX", P_tmpdir);
  61.     return(mktemp(s));
  62. }
  63.  
  64. char *
  65. tempnam(dir, pfx)
  66.     char *dir, *pfx;
  67. {
  68.     char *f, *name, *getenv(), *malloc(), *mktemp();
  69.  
  70.     if (!(name = malloc((u_int)MAXPATHLEN)))
  71.         return(NULL);
  72.  
  73.     if (f = getenv("TMPDIR")) {
  74.         (void)sprintf(name, "%s/%sXXXXXX", f, pfx ? "" : pfx);
  75.         if (f = mktemp(name))
  76.             return(f);
  77.     }
  78.     if (dir) {
  79.         (void)sprintf(name, "%s/%sXXXXXX", dir, pfx ? "" : pfx);
  80.         if (f = mktemp(name))
  81.             return(f);
  82.     }
  83.     (void)sprintf(name, "%s/%sXXXXXX", P_tmpdir, pfx ? "" : pfx);
  84.     if (f = mktemp(name))
  85.         return(f);
  86.     (void)sprintf(name, "/tmp/%sXXXXXX", pfx ? "" : pfx);
  87.     if (!(f = mktemp(name)))
  88.         (void)free(name);
  89.     return(f);
  90. }
  91.